package com.jwgf.common;

public class DataAccessRule {
//	public static final int BYTE_TYPE = 1;
	public static final int SHORT_TYPE = 2;
	public static final int INT_TYPE = 3;
	public static final int FLOAT_TYPE = 4;
	public static final int STRING_TYPE = 5;

	public String key;
	public int dataType;
//	public byte min_byte;
//	public byte max_byte;
	public short min_short;
	public short max_short;
	public int min_int;
	public int max_int;
	public float min_float;
	public float max_float;
	public int defaultIntValue;
	public float defaultFloatValue;
	public String defualtStringValue;
	public String precision;

	public DataAccessRule(String key, int dataType, int min_int, int max_int,
			int defaultValue) {
		super();
		this.key = key;
		this.dataType = dataType;
		this.min_int = min_int;
		this.max_int = max_int;
		this.defaultIntValue = defaultValue;
	}

	public DataAccessRule(String key, int dataType, float min_float,
			float max_float, String precision, float defaultValue) {
		super();
		this.key = key;
		this.dataType = dataType;
		this.min_float = min_float;
		this.max_float = max_float;
		this.precision = precision;
		this.defaultFloatValue = defaultValue;
	}

	public DataAccessRule(String key, int dataType, String defaultValue) {
		super();
		this.key = key;
		this.dataType = dataType;
		this.defualtStringValue = defaultValue;
	}
	
	public boolean isBetween(String value) {
		switch (dataType) {
/*		case BYTE_TYPE: {
			byte data = Byte.parseByte(value);
			if (data >= min_byte && data <= max_byte) {
				return true;
			} else {
				return false;
			}
		}*/
		case SHORT_TYPE: {
			short data = Short.parseShort(value);
			if (data >= min_short && data <= max_short) {
				return true;
			} else {
				return false;
			}
		}
		case INT_TYPE: {
			int data = Integer.parseInt(value);
			if (data >= min_int && data <= max_int) {
				return true;
			} else {
				return false;
			}
		}
		case FLOAT_TYPE: {
			float data = Float.parseFloat(value);
			if (data >= min_float && data <= max_float) {
				return true;
			} else {
				return false;
			}
		}
		}
		return false;
	}
	
	public String getMin() {
		return (dataType == INT_TYPE) ? String.valueOf(min_int) : String
				.valueOf(min_float);
	}

	public String getMax() {
		return (dataType == INT_TYPE) ? String.valueOf(max_int) : String
				.valueOf(max_float);
	}

	public String getDefaultValue() {
		String str = "";
		switch (dataType) {
		case SHORT_TYPE:
			str = String.valueOf(defaultIntValue);
			break;
		case INT_TYPE:
			str = String.valueOf(defaultIntValue);
			break;
		case FLOAT_TYPE:
			str = String.valueOf(defaultFloatValue);
			break;
		}
		return str;
	}
}
